var canvas = document.querySelector('.myCanvas');
var ctx = canvas.getContext('2d');

const noRows = 15;
const noCols = 10;
const squareSize = 50;
const lineW = 1;
const height = canvas.height;
const width = canvas.width;
let h = 0;
let w = 1;
let editorX = 90;
let editorY = 25;
let a;
let figurki = [];
figurki.push([[0, 5], [0, 4], [1, 5], [1, 4], 1]); // квадрат
figurki.push([[0, 3], [0, 4], [0, 5], [0, 6], 0]); // палка
figurki.push([[0, 5], [1, 5], [2, 5], [2, 4], 2]); // Г
figurki.push([[0, 5], [1, 5], [2, 5], [2, 6], 2]); // зеркальная Г
figurki.push([[1, 4], [1, 5], [0, 5], [1, 6], 0]); // T
figurki.push([[1, 4],[1,5],[0,5],[0,6], 0]);       // Z
figurki.push([[0, 4], [0, 5], [1, 5], [1, 6], 0]); // S
let move = 0;
let pov;
generate_basic_table();

function getRandomInt(max) {
  return Math.floor(Math.random() * max);
}

function generate_basic_table() {
  a = [];
  for (let i = 0; i < 15; i++) {
      a.push([]);
      for (let j = 0; j < 10; j++){
          a[i].push(0);
      }
  }
  dobavit_figurku();
  return a;
}

function povorot() {
    let figura = [];
    let new_figura = [];
    let i0 = -1;
    let j0 = -1;
    for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 10; j++){
            if (a[i][j] === 2 && figura.length === 0) {
                figura.push([i, j]);
                new_figura.push([i, j]);
                i0 = i;
                j0 = j;
                }
                }
                }
     for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 10; j++){
            if (a[i][j] === 1) {
                figura.push([i, j]);
                let i1 = i0 + j - j0;
                let j1 = j0 + (i - i0);
                console.log(pov);
                if (!(i1 >= 0 && i1 <= 14 && j1>=0 && j1 <= 9 && a[i1][j1] !== -1)) {
                    console.log("can't rotate, i= " +  i1 + ", j=" + j1);
                    return;
                }
                new_figura.push([i1, j1]);
            }
        }
     }
    for (let k = 0; k < figura.length; k++) {
        let i = figura[k][0];
        let j = figura[k][1];
        a[i][j] = 0;
    }
    for (let k = 0; k < new_figura.length; k++) {
        let i = new_figura[k][0];
        let j = new_figura[k][1];
        if (k === 0) {
            a[i][j] = 2;
        } else {
        a[i][j] = 1;
        }
    }
    pov *= -1;
    console.log(new_figura);
    console.log(figura);
}

function dobavit_figurku() {
    let figura=figurki[getRandomInt(figurki.length)];
    for (let k = 0; k < figura.length - 1; k++){
        let i = figura[k][0];
        let j = figura[k][1];
    if (a[i][j] == -1){
        alert("Игра закончилась")
        return;
    }
        }

    for (let k = 0; k < figura.length - 1; k++){
        let i = figura[k][0];
        let j = figura[k][1];
        a[i][j] = 1;
    }
     let i = figura[figura[figura.length-1]][0];
     let j = figura[figura[figura.length-1]][1];
     a[i][j] = 2;
    pov = 1;

}

function print_table() {
   for (let i = 0; i < 15; i++) {
      let s = "";
      for (let j = 0; j < 10; j++){
      s += "| " + a[i][j] + " ";
}
  s += "|\n";
  console.log(s);
}
  console.log("\n")
}


function drawMap()
{
    for(let row = 0; row < noRows; row++)
    {
        for(let col = 0; col < noCols; col++)
        {
            let cellX = editorX + col * squareSize;
            let cellY = editorY + row * squareSize;
            ctx.fillStyle = "#FFFFFF";
            ctx.lineWidth = lineW;
            ctx.fillRect(cellX, cellY, squareSize, squareSize);
            ctx.strokeStyle = "black";
            ctx.strokeRect(cellX, cellY, squareSize, squareSize);
            ctx.font = squareSize + "px serif";
            if ( a[row][col] === -1) {
                num = -1;
                ctx.fillStyle = "blue";
            } else if (a[row][col] === 1) {
                num = 1;
                  ctx.fillStyle = "green";
            } else if ( a[row][col] === 0) {
              num = 0;
              ctx.fillStyle = "black";
          } else if (a[row][col] === 2) {
           num = 2;
              ctx.fillStyle = "red";
          }
            ctx.textAlign = "center";
            ctx.textBaseline = "bottom";
            ctx.fillText(num, cellX+squareSize/2, cellY+squareSize);
        }
    }
}


function loop()
{
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    novoe_pole();
    drawMap();
}

function vpravo() {
    for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 10; j++){
            if ((a[i][j] === 1) && !((j !== 14) && (a[i][j+1] !== -1)))  {
                return;
            }
        }
    }
    for (let i = 0; i < 15; i++) {
        for (let j = 9; j >= 0; j--){
            if (j > 0 && a[i][j-1] > 0) {
                a[i][j] = a[i][j-1];
            } else if (a[i][j] > 0) {
                a[i][j] = 0;
            }
        }
    }
}

function vlevo() {
    for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 10; j++){
            if ((a[i][j] === 1) && !((j !== 0) && (a[i][j-1] !== -1)))  {
                return;
            }
        }
    }
    for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 10; j++){
            if (j != 9 && a[i][j+1] > 0) {
                a[i][j] = a[i][j+1];
            } else if (a[i][j] > 0) {
                a[i][j] = 0;
            }
        }
    }
}

function novoe_pole() {
    if (move === 2) {
    povorot();
    }else if (move === 1) {
        vpravo();
    } else if (move === -1) {
        vlevo();
    }
    move = 0;
    let OK_dvigat = true;
    for (let i = 0; i < 15; i++) {
        for (let j = 0; j < 10; j++){
            if ((a[i][j] > 0) && !((i !== 14) && (a[i+1][j] !== -1))) {
                OK_dvigat = false;
            }
        }
    }
    if (OK_dvigat === false) {
        for (let i = 0; i < 15; i++) {
            for (let j = 0; j < 10; j++){
                if (a[i][j] >0) {
                    a[i][j] = -1;
                }
            }
        }
        dobavit_figurku();
        return;
    }
    for (let i = 14; i > 0; i--) {
        for (let j = 0; j < 10; j++){
            if (a[i-1][j] > 0) {
                a[i][j] = a[i-1][j];
                a[i-1][j] = 0;   
            }
        }
    }
}



window.addEventListener("keydown",(event) => {
  if (event.isComposing || event.keyCode === 229) {
    return;
  }
  if (event.keyCode === 39) {
    move = 1;
  } else if (event.keyCode === 37) {
  move = -1;
   } else if (event.keyCode === 68) {
   move = 2;
   }

});

let timer = setInterval(loop, 1000);